<?php
namespace app\service\data;

use app\service\models as models;
use yii;
use app\lib\Code;

class User {
    
    private $_dsUser = null;
    
    private $_codeRedisKey = 'oct|sms|code|%s'; //手机号码
    
//     private $_useridRedis
    
    public function __construct() {
        $this->_dsUser = new  models\User();
    }
    
    //用户注册
    public function Register($arrInput) {
        $phone = $arrInput['phone'];
        //检测验证码是否正确
        $redisKey = sprintf($this->_codeRedisKey, $phone);
        $code = Yii::$app->cache->get($redisKey);
        if(!$code) {
            formatJson(Code::PARAM, '验证码已过期');
        }
        if($code != $arrInput['verify']) {
            formatJson(Code::PARAM, '验证码不正确');
        }
        
        //校验手机号是否存在
        $ret = $this->_dsUser->find()->where(['phone'=>$arrInput['phone']])->one();
        if($ret) {
            formatJson(Code::PARAM, '手机号已存在');
        }
        
        $this->_dsUser->phone = $arrInput['phone'];
        $this->_dsUser->hospital_name = $arrInput['hospitalname'];
        $this->_dsUser->email = $arrInput['email'];
        $ret = $this->_dsUser->save();
        
        //设置登录状态
        $loginret = $this->_dsUser->find()->where(['phone'=>$phone])->one();
        if(!$loginret) {
            formatJson(Code::PARAM, '登录失败');
        }
        
        $this->setLoginStatus($loginret->id);
        
        return true;
    }
    
    //登录
    public function Login($arrInput) {
        $phone = $arrInput['phone'];
        //校验验证码
        $redisKey = sprintf($this->_codeRedisKey, $phone);
        $code = Yii::$app->cache->get($redisKey);
        if(!$code) {
            formatJson(Code::PARAM, '验证码已过期');
        }
        if($code != $arrInput['verify']) {
            formatJson(Code::PARAM, '验证码不正确');
        }
        
        //校验手机号是否存在
        $ret = $this->_dsUser->find()->where(['phone'=>$arrInput['phone']])->one();
        if(!$ret) {
            formatJson(Code::PARAM, '该用户不存在，请注册');
        }
        
        $this->setLoginStatus($ret->id);
        
        return true;
    }
    
    private function setLoginStatus($uid) {
        //登录成功，设置
        yii::$app->session['uid'] = $uid;
        
        $cookie = new \yii\web\Cookie();
        $cookie->name = 'uid';
        $cookie->value = $uid;
        $cookie->expire = time() + 6*60*60; //6小时过期时间
        $cookie->httpOnly = false;
        
        Yii::$app->response->cookies->add($cookie);
        Yii::$app->response->send();
        
        //设置过期时间
    }
    
    /**
     * 后台-用户列表
     */
    public function UserList($arrInput) {
        $count = $this->_dsUser
        ->find()
        ->count();
        $ret = $this->_dsUser
        ->find()
        ->offset(($arrInput['page'] - 1) * $arrInput['page_size'])
        ->limit($arrInput['page_size'])
        ->all();
        
        $data_list = [];
        
        foreach ($ret as $value) {
            $data_list[] = $value->toArray([
                'id',
                'phone',
                'hospital_name',
                'create_time',
                'email',
            ]);
        }
        
        return [
            "total_size"=>intval($count),
            "page"=>$arrInput['page'],
            "page_size"=>$arrInput['page_size'],
            "data_list"=>$data_list,
        ];
    }
}